package com.liuym.admin.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.liuym.admin.repository.model.vo.StudentVO;
import com.liuym.admin.service.TableNameService;
import com.liuym.admin.util.StudentScoreTableNameUtil;
import com.liuym.common.annotation.AdminLogin;
import com.liuym.common.constants.Constants;
import com.liuym.common.constants.GradeResponse;
import com.liuym.common.dubbo.student.api.StudentDubboService;
import com.liuym.common.dubbo.student.api.StudentScoreDubboService;
import com.liuym.common.dubbo.student.dto.StudentDTO;
import com.liuym.common.dubbo.student.dto.StudentScoreDTO;
import com.liuym.common.exception.StudentException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Liuym
 * @date 2018/12/10 13:38
 * @describe：
 */

@AdminLogin
@RestController
@RequestMapping("/admin/student")
public class StudentController {

    private static Logger logger = LoggerFactory.getLogger(StudentController.class);


    @Reference(version = "${student.dubbo.version}")
    private StudentDubboService studentDubboService;

    @Reference(version = "${student.dubbo.version}")
    private StudentScoreDubboService studentScoreDubboService;

    @Autowired
    private TableNameService tableNameService;

    @GetMapping("/findStudentByStudentNumber")
    public GradeResponse findStudentByStudentNumber(long studentNumber) {
        StudentVO studentVO = new StudentVO();
        try {
            StudentDTO studentDTO = studentDubboService.getStudentByNumber(studentNumber);
            List<StudentScoreDTO> studentScoreDTOList = studentScoreDubboService.getStudentScoreByStudentNumber(
                    tableNameService.findByTableAndTime(Constants.STUDENT_SCORE_TABLE_NAME).getTableName(),
                    studentNumber);

            studentVO.setName(studentDTO.getName());
            studentVO.setStudentNumber(studentDTO.getStudentNumber());
            studentVO.setScore(studentDTO.getScore());
            studentVO.setClassNumber(studentDTO.getClassNumber());
            studentVO.setStudentScoreList(studentScoreDTOList);
            studentVO.setDuty(studentDTO.getDuty());
        } catch (StudentException e) {
            logger.error("error", e);
            return GradeResponse.internalServerError();
        }
        return GradeResponse.success(studentVO);
    }

    @GetMapping("/findAllStudent")
    public GradeResponse findAllStudent() {
        List<StudentDTO> allStudent = null;
        try {
            allStudent = studentDubboService.getAllStudent();
        } catch (StudentException e) {
            logger.error("error", e);
            return GradeResponse.internalServerError();
        }
        return GradeResponse.success(allStudent);
    }

    @PostMapping("/findStudentByClassNumber")
    public GradeResponse findStudentByClassNumber(@RequestParam("classNumber") long classNumber) {
        List<StudentDTO> studentDTOList = null;
        try {
            studentDTOList = studentDubboService.getStudentByClass(classNumber);
        } catch (StudentException e) {
            logger.error("error", e);
            return GradeResponse.internalServerError();
        }
        return GradeResponse.success(studentDTOList);
    }

    @PostMapping("/addStudent")
    public GradeResponse addStudent(StudentDTO studentDTO) {
        int result = 0;
        try {
            result = studentDubboService.addStudent(studentDTO);
        } catch (Exception e) {
            if (e instanceof StudentException) {
                logger.warn("warn", e);
                return GradeResponse.fail(((StudentException) e).getCode(), ((StudentException) e).getMsg());
            } else {
                logger.error("error", e);
                return GradeResponse.internalServerError();
            }
        }
        return GradeResponse.success(result);
    }

    @DeleteMapping("/deleteStudent")
    public GradeResponse deleteStudent(long studentNumber) {
        int result = 0;
        try {
            result = studentDubboService.deleteStudentByStudentNumber(studentNumber);
        } catch (StudentException e) {
            logger.error("error", e);
            return GradeResponse.internalServerError();
        }
        return GradeResponse.success(result);
    }

    @PutMapping("/updateStudent")
    public GradeResponse uptateStudent(StudentDTO studentDTO) {
        int result = 0;
        try {
            result = studentDubboService.updateStudent(studentDTO);
        } catch (Exception e) {
            if (e instanceof StudentException) {
                logger.warn("warn", e);
                return GradeResponse.fail(((StudentException) e).getCode(), ((StudentException) e).getMsg());
            }
            logger.error("error", e);
            return GradeResponse.internalServerError();
        }
        return GradeResponse.success(result);
    }

    @PutMapping("/resetStudent")
    public GradeResponse resetStudent() {
        try {

            StudentScoreTableNameUtil.setStudentScoreTableName(StudentScoreTableNameUtil.getStudentScoreTableName());
            try {
                studentScoreDubboService.addStudentScoreTable(StudentScoreTableNameUtil.getStudentScoreTableName().toString());
            } catch (StudentException e) {
                logger.warn("请不要重复操作！", e);
                return GradeResponse.fail(e.getCode(), e.getMsg());
            } finally {
                StudentScoreTableNameUtil.getStudentScoreTableName().delete(15, StudentScoreTableNameUtil.getStudentScoreTableName().length());
            }
            studentDubboService.updateStudentScoreTo80();

        } catch (Exception e) {
            logger.error("调用updateStudentScoreTo80失败！", e);
            return GradeResponse.internalServerError();
        }
        return GradeResponse.success();
    }
}
